#ifndef BASE_CTLOG_H_ 
#define BASE_CTLOG_H_ 

#include "ct_thread.h"
#include "ct_mutex.h"
#include <stdio.h> 
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <fstream> 
#include <string> 
#include <time.h>
#include <unistd.h>

class ctLOG : public ctThread
{
        public:
                static const int LOG_NAME_TYPE = 14;
                enum eLogLevel
                {
                        eLogLevel_Panic = 0,
                        eLogLevel_Fatal,
                        eLogLevel_Error,
                        eLogLevel_Warning,
                        eLogLevel_Info,
                        eLogLevel_Debug,
                        eLogLevel_Trace,
                        eLogLevel_All,
                        eLogLevel_Off,
                };

        public:   
                ctLOG();
                ~ctLOG(); 

                bool init(const std::string filename, const std::string &logprefix);
                void setLogPrefix(const std::string &logprefix) { log_prefix_ = logprefix; }
                std::string getLogPrefix() const { return log_prefix_; }
                void setLogLevel(eLogLevel level) { global_log_level_ = level; }
                eLogLevel getLogLevel() const { return global_log_level_; }
                const char *getLogLevelStr(const eLogLevel level);

                void stop();
                void output(char *buf, int cnt);  

                void panic(const char *fmt, ...)__attribute__((format(printf, 2,3)));  
                void fatal(const char *fmt, ...)__attribute__((format(printf, 2,3)));    
                void error(const char *fmt, ...)__attribute__((format(printf, 2,3)));   
                void warn(const char *fmt, ...)__attribute__((format(printf, 2,3)));   
                void info(const char *fmt, ...)__attribute__((format(printf, 2,3)));
                void debug(const char *fmt, ...)__attribute__((format(printf, 2,3))); 
                void trace(const char *fmt, ...)__attribute__((format(printf, 2,3)));

        private: 
                int FormateTime(char *buf, eLogLevel type);
                bool CreateLogFile(const std::string &log_file);
                int CommonLog(eLogLevel loglevel, char* buf, char *fmt, ...);
                bool LevelControl(const eLogLevel &level);
                void run(); //complete thread function callback

                int log_fd_; 
                std::string log_file_name_;
                std::string next_file_name_;
                std::string log_prefix_;
                eLogLevel global_log_level_;
                ctMutex lock_;
                int log_size_; //not use
                int rotate_time_flag_; 
};   
#endif //BASE_CTLOG_H_
